//
//  IncubationSpaceDetailsViewController.m
//  BigToBig
//
//  Created by 毛丽媛 on 16/8/1.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "IncubationSpaceDetailsViewController.h"
#import "DetailInfoCell.h"
#import "FeaturesServiceCell.h"
#import "MapTableViewCell.h"

@interface IncubationSpaceDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,copy)NSString *info;
@property(nonatomic,strong)UILabel *titleLab;
@property(nonatomic,strong)UILabel *industry;
@property(nonatomic,strong)UIScrollView *picView;
@property(nonatomic,strong)NSArray *imageArr;
@property(nonatomic,strong)UILabel *area;
@property(nonatomic,strong)UILabel *price;

@property(nonatomic,strong)NSMutableArray *wordArr;
@property(nonatomic,strong)NSMutableArray *picArr;

@end

@implementation IncubationSpaceDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLab.text = @"孵化空间";
    titleLab.textColor = [UIColor whiteColor];
    [self setBackBar];
    titleLab.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLab;
    self.navigationController.navigationBar.barTintColor = COLOR_RGBA(77, 155, 255, 1);
    self.view.backgroundColor=GRAYCOLOR;
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = NO;
    self.tableView.bounces = NO;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[DetailInfoCell class] forCellReuseIdentifier:@"cell"];
    
    [self.tableView registerClass:[FeaturesServiceCell class] forCellReuseIdentifier:@"feature"];
    
    [self.tableView registerClass:[MapTableViewCell class] forCellReuseIdentifier:@"map"];
    
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight / 2 - 30)];
    [self.view addSubview:headView];
    
    self.tableView.tableHeaderView = headView;
    
    self.picView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, headView.maxY - 100)];
    // self.picView.backgroundColor = [UIColor yellowColor];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, headView.maxY - 80)];
    image.image = [UIImage imageNamed:@"3.jpg"];
    _picView.contentSize = CGSizeMake(3 * kScreenWidth, 0);
    _picView.pagingEnabled = YES;
    _picView.bounces = NO;
    _picView.contentOffset = CGPointMake(kScreenWidth, 0);
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changImage) userInfo:nil repeats:YES];
    _picView.delegate = self;
    _picView.tag = 1001;
    _picView.showsHorizontalScrollIndicator = NO;
    
    [headView addSubview:self.picView];
    
    //公司
    self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake( 20, self.picView.maxY + 10, kScreenWidth - 40, 30)];
    self.titleLab.textAlignment = NSTextAlignmentCenter;
    self.titleLab.text = @"北京天汇航天科技孵化器公司";
    [headView addSubview:self.titleLab];
    
    //描述
    self.industry = [[UILabel alloc] initWithFrame:CGRectMake(20, self.titleLab.maxY, kScreenWidth - 40, 30)];
    self.industry.textAlignment = NSTextAlignmentCenter;
    self.industry.text = @"免费提供上下行10G网络, 饮水机";
    self.industry.font = [UIFont systemFontOfSize:14];
    self.industry.textColor = [UIColor lightGrayColor];
    [headView addSubview:self.industry];
    
    //面积
    self.area = [[UILabel alloc] initWithFrame:CGRectMake(20, self.industry.maxY, kScreenWidth / 2 - 25, 20)];
    self.area.textAlignment = NSTextAlignmentRight;
    self.area.text = @"200m²面积";
    self.area.font = [UIFont systemFontOfSize:11];
    self.area.textColor = [UIColor lightGrayColor];
    [headView addSubview:self.area];
    
    //价钱
    self.price = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 2 + 5, self.industry.maxY, kScreenWidth / 2 - 5, 20)];
    self.price.textAlignment = NSTextAlignmentLeft;
    self.price.text = @"10000元/平米/年";
    self.price.font = [UIFont systemFontOfSize:11];
    self.price.textColor = [UIColor lightGrayColor];
    [headView addSubview:self.price];
    
    
    //self.imageArr = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
    
    //通过for循环创建对象,然后通过是从来了VIew上显示
    for (NSInteger i = 0; i < 3; i++) {
        
        NSString *imageName = [NSString stringWithFormat:@"%ld.jpg", i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * i , 0, kScreenWidth, kScreenHeight / 2 - 50)];
        //[imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[i]]];
        imageView.image = image;
        UIImageView *lastImage = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * 3, 0, kScreenWidth, kScreenHeight / 2 - 50)];
        //[lastImage sd_setImageWithURL:[NSURL URLWithString:self.imageArr[0]]];
        lastImage.image = [UIImage imageNamed:@"0.jpg"];
        [self.picView addSubview:imageView];
        [self.picView addSubview:lastImage];
    }
    
    self.wordArr = [NSMutableArray arrayWithObjects:@"人才公寓",@"路演厅",@"税务代理",@"会议室",@"行政帮助",@"公司注册",@"停车位",@"创业辅导",@"投融资对接",@"文印一体",@"营销推广", nil];
    
    self.picArr = [NSMutableArray arrayWithObjects:@"家庭公寓",@"演播厅",@"小规模纳税人企业代理记账",@"会议厅",@"行政帮助",@"公司注册",@"停车位",@"创业辅导",@"投融资对接",@"文印一体",@"营销推广", nil];
   
    /**
     *  获取数据
     */
    [self createDate];
    
}
/**
 *  数据
 */
- (void)createDate{
    [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/info/search.json?site_id=0&offset=0&each=20" params:nil success:^(id json) {
        
        
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)changImage{
    //先找到对应的scrollView
    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:1001];
    [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x + kScreenWidth, 0) animated:YES];
    if (scrollView.contentOffset.x == 3 * kScreenWidth) {
        scrollView.contentOffset = CGPointZero;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    view.backgroundColor = [UIColor colorWithWhite:0.949 alpha:1.000];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth - 20, 50)];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:titleLabel];
    if (section == 0) {
        titleLabel.text = @"服务描述";
    }if (section == 1) {
        titleLabel.text = @"特色服务";
    }if (section == 2) {
        titleLabel.text = @"地图位置";
    }
    return view;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        DetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        self.info = @"赶紧换地方聚会时过分 粉丝福建省及发放速度就放开手健康的双方开始的疯狂分局新客户反馈及房价快速的回复后回家的返回合肥师范恢复速度和粉丝的回复分毒素和飞机恢复速度换房间和大家是否就是孵化附近的思考方式的方式加速度和数据库";
        cell.infoLabel.text = self.info;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
     if (indexPath.section == 1){
        FeaturesServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"feature" forIndexPath:indexPath];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
         
        return cell;
     }else{
         MapTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"map" forIndexPath:indexPath];
         tableView.rowHeight = 340;
         return cell;
     }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];
        // 根据文本的内容和文本的字体进行计算高度
        // 参数一:告诉系统,文本显示的最大范围
        CGFloat width = (self.view.frame.size.width - 40);
        CGRect rect = [self.info boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        // 然后把图片的高和文本的高作为返回值返回
        return rect.size.height + 20;
    }if (indexPath.section == 1){
        if (self.wordArr.count % 3 == 0) {
            return self.wordArr.count / 3 * (kScreenWidth - 60)/3 + (self.wordArr.count/3) * 30;
        }
        else{
            return (self.wordArr.count / 3 + 1) * (kScreenWidth - 60)/3 + (self.wordArr.count/3 + 1) * 30;

        }
    }else{
        return kScreenHeight/4;
    }
}

//#pragma mark 刷新某一个cell
//- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0){
//    NSIndexPath *indexPath_1=[NSIndexPath indexPathForRow:0 inSection:1];
//    NSArray *indexArray=[NSArray arrayWithObject:indexPath_1];
//    [_tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];
////    self.wordArr = [NSMutableArray arrayWithObjects:@"人才公寓",@"路演厅",@"税务代理",@"会议室",@"行政帮助",@"公司注册",@"停车位",@"创业辅导",@"投融资对接",@"文印一体",@"营销推广", nil];
////    
////    self.picArr = [NSMutableArray arrayWithObjects:@"家庭公寓",@"演播厅",@"小规模纳税人企业代理记账",@"会议厅",@"行政帮助",@"公司注册",@"停车位",@"创业辅导",@"投融资对接",@"文印一体",@"营销推广", nil];
//}


#pragma mark 导航栏左右Button
- (void)setBackBar{
    
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
}

- (void)backController:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
    //self.tabBarController.tabBar.hidden = NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
